package com.jinghangzz.shop.head.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.jinghangzz.shop.common.controller.BaseController;
import com.jinghangzz.shop.common.util.ConstatFinalUtil;
import com.jinghangzz.shop.orders.pojo.AOrders;
import com.jinghangzz.shop.orders.pojo.AOrdersGoods;
import com.jinghangzz.shop.orders.service.IOrdersService;
import com.jinghangzz.shop.users.pojo.AUsers;
import com.jinghangzz.shop.users.pojo.AUsersAddress;
import com.jinghangzz.shop.users.pojo.AUsersCar;
import com.jinghangzz.shop.users.service.IUsersService;

@Controller
@RequestMapping("/head/orders")
public class OrdersHeadController extends BaseController
{
	@Resource
	private IUsersService usersService;
	@Resource
	private IOrdersService ordersService;
	
	/**
	 * 订单列表
	 * @return
	 */
	@RequestMapping("/ordersList")
	public String ordersList()
	{
		ConstatFinalUtil.SYS_LOGGER.info("==ordersList==");
		return "/head/orders/ordersList";
	}
	
	/**
	 * 打开下单
	 * @return
	 */
	@RequestMapping("/ordersInsert")
	public String ordersInsert(HttpServletRequest request)
	{
		ConstatFinalUtil.SYS_LOGGER.info("==ordersInsert==");
		HttpSession session = request.getSession() ; 
		AUsers usersSess = (AUsers) session.getAttribute("users");
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 接收用户选择了几件商品
		 * 含义是:用户购物车id
		 *  */
		String[] ucids = request.getParameterValues("ucid");
		List<AUsersCar> usersCarList = new ArrayList<AUsersCar>();
		if(ucids != null)
		{
			for (int i = 0; i < ucids.length; i++)
			{
				String ucidTemp = ucids[i];
				/* 去查询购物车 */
				condMap.clear();
				condMap.put("id", ucidTemp);
				AUsersCar usersCar = this.usersService.findOneUsersCarService(condMap);
				usersCarList.add(usersCar);
			}
		}
		request.setAttribute("usersCarList", usersCarList);
		
		/* 查询当前用户所有启用的收货地址 */
		condMap.clear();
		condMap.put("usersId", usersSess.getId());
		condMap.put("status", "1");
		List<AUsersAddress> usersAddressList = this.usersService.findCondListUsersAddressService(null, condMap);
		request.setAttribute("usersAddressList", usersAddressList);
		return "/head/orders/ordersInsert";
	}
	
	/**
	 * 下单提交
	 * @return
	 */
	@RequestMapping("/ordersInsertSubmit")
	public String ordersInsertSubmit(AOrders orders,HttpServletRequest request)
	{
		ConstatFinalUtil.SYS_LOGGER.info("==ordersInsertSubmit==");
		HttpSession session = request.getSession() ; 
		AUsers usersSess = (AUsers) session.getAttribute("users");
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 将订单信息保存到数据库 */
		String[] ucids = request.getParameterValues("ucid");
		List<AOrdersGoods> ordersGoodList = new ArrayList<AOrdersGoods>();
		if(ucids != null)
		{
			for (int i = 0; i < ucids.length; i++)
			{
				String ucidTemp = ucids[i];
				/* 去查询购物车 */
				condMap.clear();
				condMap.put("id", ucidTemp);
				AUsersCar usersCar = this.usersService.findOneUsersCarService(condMap);
				
				/* 准备一个订单明细 */
				AOrdersGoods ordersGoods = new AOrdersGoods();
				
				ordersGoods.setGoodsId(usersCar.getGoodsId());
				ordersGoods.setNum(usersCar.getNum());
				
				ordersGoodList.add(ordersGoods);
			}
		}
		
		/* 要为一些驱动赋值失败的进行赋值 */
		orders.setUsersId(usersSess.getId());
		orders.setOrdersGoodList(ordersGoodList);
		
		/* 所有的工作交给Service进行处理,因为Serivce有事务管理器
		 * 同时操作三张表;
		 *  */
		JSONObject resultJSON = this.ordersService.saveOneOrdersService(orders);
		ConstatFinalUtil.SYS_LOGGER.info("==保存订单结果:{}==",resultJSON.toJSONString());
		return this.ordersInsert(request);
	}
}
